package com.example.sunsg.okhttptest.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.Toast;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.adapter.SpotEditAdapter;
import com.example.sunsg.okhttptest.bean.LocalImageItemImage;
import com.example.sunsg.okhttptest.bean.PreviewImage;
import com.example.sunsg.okhttptest.bean.SpotEditItem;
import com.example.sunsg.okhttptest.bean.SpotEditItemDescription;
import com.example.sunsg.okhttptest.bean.SpotEditItemImage;
import com.example.sunsg.okhttptest.bean.SpotEditItemOtherInfo;
import com.example.sunsg.okhttptest.bean.SpotEditItemPlue;
import com.example.sunsg.okhttptest.util.Util;

import java.util.ArrayList;
import java.util.List;


public class SpotEditActivity extends AppCompatActivity {
    public static final String KEY_IMAGE_LIST = "key_image_list";
    /**menu group index*/
    private static final int MENU_INDEX = 0;
    /**menu group index*/
    private static final int MENU_ITEM_ORDER = 100;
    /**完成id*/
    private static final int ID_FISISH = 0x1;
    /**预览id*/
    private static final int ID_PREVIEW = 0x2;

    private RecyclerView mRecyclerView;
    private SpotEditAdapter mSpotEditAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_spot);
        initView();
    }



    public static void launch(Activity activity, ArrayList<LocalImageItemImage> images){
        Intent intent = new Intent(activity,SpotEditActivity.class);
        intent.putParcelableArrayListExtra(KEY_IMAGE_LIST, (ArrayList<? extends Parcelable>) images);
        activity.startActivity(intent);
    }

    private void initView(){
        toolBar();
        mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(manager);

        mSpotEditAdapter = new SpotEditAdapter(getApplicationContext());
        mRecyclerView.setAdapter(mSpotEditAdapter);
        addData();
    }

    private void addData(){
//        addTestData();
        Intent intent = getIntent();
        if(intent != null){
            List<LocalImageItemImage> images = getIntent().getParcelableArrayListExtra(KEY_IMAGE_LIST);
            List<SpotEditItem> items = new ArrayList<>();
            //添加空的描述
            SpotEditItem item = new SpotEditItem(SpotEditItem.TYPE_DESCRIPTION,new SpotEditItemDescription());
            items.add(item);
            if(images != null){
                for (LocalImageItemImage image : images){
                    //添加 ＋ 号
                    item = new SpotEditItem(SpotEditItem.TYPE_PLUE,new SpotEditItemPlue());
                    items.add(item);
                    //添加image
                    item = new SpotEditItem(SpotEditItem.TYPE_IMAGE,new SpotEditItemImage(image));
                    items.add(item);
                }
            }

            //添加描述
            item = new SpotEditItem(SpotEditItem.TYPE_OTHER_INFO,new SpotEditItemOtherInfo(Util.getFormatTime(System.currentTimeMillis(),"yyyy.MM.dd HH:mm")));
            items.add(item);
            mSpotEditAdapter.setItems(items);
        }


    }

    private void toolBar(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar ab = getSupportActionBar();
        ab.setTitle(getString(R.string.edit_spot)+"绝世独立加夫里什地方极乐世界分；山东飞机失联飞机撒酒疯");
        ab.setHomeAsUpIndicator(R.drawable.im_back);
        ab.setDisplayHomeAsUpEnabled(true);
    }

    private void bindEvent(){
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
    private void addTestData(){
        List<SpotEditItem> items  = new ArrayList<>();
        SpotEditItem item = new SpotEditItem(SpotEditItem.TYPE_DESCRIPTION,new SpotEditItemDescription());
        SpotEditItem item1 = new SpotEditItem(SpotEditItem.TYPE_PLUE,new SpotEditItemPlue());
        SpotEditItem item2 = new SpotEditItem(SpotEditItem.TYPE_IMAGE,new SpotEditItemImage());
        SpotEditItem item3 = new SpotEditItem(SpotEditItem.TYPE_PLUE,new SpotEditItemPlue());
        SpotEditItem item4 = new SpotEditItem(SpotEditItem.TYPE_IMAGE,new SpotEditItemImage());
        SpotEditItem item5 = new SpotEditItem(SpotEditItem.TYPE_PLUE,new SpotEditItemPlue());
        SpotEditItem item6 = new SpotEditItem(SpotEditItem.TYPE_IMAGE,new SpotEditItemImage());
        SpotEditItem item7 = new SpotEditItem(SpotEditItem.TYPE_PLUE,new SpotEditItemPlue());
        SpotEditItem item8 = new SpotEditItem(SpotEditItem.TYPE_IMAGE,new SpotEditItemImage());
        SpotEditItem item9 = new SpotEditItem(SpotEditItem.TYPE_PLUE,new SpotEditItemPlue());
        SpotEditItem item10 = new SpotEditItem(SpotEditItem.TYPE_OTHER_INFO,new SpotEditItemOtherInfo());
        items.add(item);
        items.add(item1);
        items.add(item2);
        items.add(item3);
        items.add(item4);
        items.add(item5);
        items.add(item6);
        items.add(item7);
        items.add(item8);
        items.add(item9);
        items.add(item10);
        mSpotEditAdapter.setItems(items);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_edit_spot, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        menu.add(MENU_INDEX,ID_FISISH,MENU_ITEM_ORDER,getString(R.string.display_exit_edit));
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.action_settings:
                break;
            case android.R.id.home:
                onBackPressed();
                break;
            case ID_FISISH:
                Toast.makeText(getApplicationContext(),"finish",Toast.LENGTH_SHORT).show();
                break;
            case ID_PREVIEW:
                Toast.makeText(getApplicationContext(),"preview",Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }


}
